package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: leyou
 * @description: 品牌服务层实现类
 * @author: NieMiao
 * @create: 2018-11-08 11:36
 **/
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;
    @Override
    public PageResult<Brand> queryListByParams(String key, String sortBy, Integer page, Integer rows, Boolean desc) {
        //分页
        PageHelper.startPage(page,rows);
        //过滤
        Example example = new Example(Brand.class);
        if (!StringUtils.isEmpty(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .andEqualTo("letter", key.toUpperCase());
        }
        //排序
        if (!StringUtils.isEmpty(sortBy)){
            String orderBy = sortBy + (desc?" DESC":" ASC");
            example.setOrderByClause(orderBy);
        }
        List<Brand> brands = brandMapper.selectByExample(example);
        PageInfo<Brand> info = new PageInfo<>(brands);
        //处理结果
        return new PageResult<>(info.getTotal(),brands);
    }
}
